<?
/**
*  封装了针对Grid和Json操作的复用方法 
*
*  2011-6-24 by zh
*/


// 字典数据定义
$dic_data = array(
		"user_status" => array(0=>"有效", -1=>"<font color=gray>无效</font>"),
		"user_flag" => array('admin'=>"管理员", 'user'=>"普通用户")
); 
    

    
/*
* 统一的Edit/Del列render
*/
function edit_del_link_mapper($v, $row) {
	return "[<a href=\"javascript:edit(".$row['id'].")\" style=\"color:blue\">修改</a>]";
	// [<a href=\"javascript:del(".$row['id'].")\" style=\"color:red\">删除</a>]";
}

	/**
	* 将数组转成json格式
	*/
function array_to_json($arr) {
	$json = "{";
	foreach($arr as $k=>$v) {
		$v = str_replace("\r", "", str_replace("\n", "<br>", $v));
		$json .= "'$k':'$v',";
	}
	$json = substr($json, 0, -1);
	$json .= "}";
	return $json;
}

/**
* 生成Flexigrid数据
*/
function to_grid_json(&$db, $sql, $filters=array(), $mapper=array(), $must_use_count=true) {
	if(isset($_GET['s']) && empty($_POST['page'])) {
		$_POST['page'] = 1; // 每次查询动作时，将页码重置到1
	}  
	$start_dt = microtime(true);
	$total = 0;
	if($must_use_count) { // 对大数据集的查询，需要强制使用count运算
		//$pattern = "/select([^from]+)from(.+)/ixs";  // 这个模式在where条件带有子查询时，将会失败！请注意
		//$countSql = preg_replace($pattern, "select count(0) as cnt from \$2", $sql); 
		$arr = explode("from", $sql);
		$countSql = "select count(0) as cnt from ".$arr[1];
		$total = $db->getOneValue($countSql);
	} else {
		$db->query($sql); // 这个只能用于小数据集下的带子查询等情况; 
		$total = $db->getRowCount();
	}

	// 判断grid前端是否有排序请求
	if(isset($_POST['sortname']) && $_POST['sortname']!='undefined' 
	&& isset($_POST['sortorder']) && $_POST['sortorder']!='undefined') {
		$pattern = "/(.+)order by(.+)/is";
		$querySql = 
		preg_replace($pattern, 
		"\$1 order by ".$_POST['sortname']." ".$_POST['sortorder'], 
		$sql); 
	} else {
		$querySql = $sql;
	}

	$querySql .= get_sql_limit();
	$db->query($querySql);

	$page = 1;
	if(isset($_POST['page'])) $page = $_POST['page'];

	$json = "";
	$json .= "{\n";
	$json .= "page: $page,\n";
	$json .= "total: $total,\n";
	$json .= "rows: [";
	$rc = false;
	$cols = null;
	$coln = 0;
	$id = 0;
	while ($db->nextRecord()) {
		$row = $db->getCurrentRecord();
		if($rc) $json .= ",";
		if(isset($row['id'])) {
			$id = $row['id'];  // 如果记录中有id字段值
		} else {
			$id++;  // 否则自动编号即可
		}
		$json .= "\n{";
		$json .= "id:'".$id."',";
		$json .= "cell:[";
		$values = '';
		foreach($row as $k=>$v) {
			if(is_int($k)) continue;
			if(array_key_exists($k, $filters)) {
				$v = $filters[$k][$v];
			}
			if(array_key_exists($k, $mapper)) {
				$func = $mapper[$k];
				$v = $func($v, $row);
			}
			$v = str_replace("\r",'',str_replace("\n",' ',$v)); // 去掉换行符
			$values .= "'". $v ."',";
		}
		$values = substr($values, 0, -1); // 去掉最后一个逗号
		$json .= $values ."]}";
		$rc = true;
	}
	$json .= "]\n";
	$json .= "}";
	return $json;
}


/**
* 返回分页限制串
*/
function get_sql_limit() {
	$page = 1;
	$rp = 15;
	if (isset($_POST['page'])) $page = intval($_POST['page']); 
	if (isset($_POST['rp'])) $rp = intval($_POST['rp']);
	$start = (($page-1) * $rp);
	$limit = " LIMIT $start, $rp";
	return $limit;
}


function blue_color_mapper($v) {
	return "<font color=blue>$v</font>";
}

function red_color_mapper($v) {
	return "<font color=red>$v</font>";
} 

function gray_color_mapper($v) {
	return "<font color=#333333>$v</font>";
} 

function green_color_mapper($v) {
	return "<font color=green>$v</font>";
} 
?>